var app = new Vue({
  el: '#app',
  data: {
    list: [{
        type: '电器',
        data: [{
            id: 1,
            select: true,
            name: 'iphone 7',
            price: 6888,
            count: 1
          },
          {
            id: 2,
            select: true,
            name: 'ipad pro',
            price: 5888,
            count: 1
          },
          {
            id: 3,
            select: true,
            name: 'MacBook Pro',
            price: 21488,
            count: 1
          },
          {
            id: 4,
            select: true,
            name: 'iphone X',
            price: 8388,
            count: 1
          }
        ]
      },
      {
        type: '水果',
        data: [{
            id: 5,
            select: true,
            name: 'apple',
            price: 12.30,
            count: 1
          },
          {
            id: 6,
            select: true,
            name: 'pear',
            price: 10.12,
            count: 1
          }
        ]
      },
      {
        type: '海鲜',
        data: [{
            id: 9,
            select: true,
            name: 'fish',
            price: 60.30,
            count: 1
          },
          {
            id: 10,
            select: true,
            name: 'inkfish',
            price: 40.30,
            count: 1
          }
        ]
      }
    ]
  },
  computed: {
    totalMound: function () {
      var totalM = 0
      for (var i = 0; i < this.list.length; i++) {
        let group = this.list[i].data
        for (let index = 0; index < group.length; index++) {
          let item = group[index]
          if (item.select)
            totalM += item.count
        }
      }
      return totalM
    },
    totalPrice: function () {
      var total = 0
      for (var i = 0; i < this.list.length; i++) {
        let group = this.list[i].data
        for (let index = 0; index < group.length; index++) {
          let item = group[index]
          if (item.select) {
            itemPr = accMul(item.price, item.count)
            console.log(item.price + "*" + item.count + "=" + itemPr)
            let curtotal = total
            total = accAdd(curtotal, itemPr)
            console.log(curtotal + "+" + itemPr + "=" + total)
          }
        }
      }
      console.log(">>>>>>>>");
      return total.toString().replace(/\B(?=(\d{3})+$)/g, ',')
    },
    selectAll: function () {
      var select = true
      for (var i = 0; i < this.list.length; i++) {
        var group = this.list[i].data
        for (let index = 0; index < group.length; index++) {
          select = group[index].select & select
        }
      }
      return select
    }
  },
  methods: {
    curindex: function (index, index2) {
      var curi = 1
      for (let i = 0; i < index; i++) {
        curi += this.list[i].data.length
      }
      return curi += index2
    },
    handleReduce: function (index, index2) {
      if (this.list[index].data[index2].count === 1) {
        return
      }
      this.list[index].data[index2].count--
    },
    handleAdd: function (index, index2) {
      this.list[index].data[index2].count++
    },
    handleRemove: function (index, index2) {
      this.list[index].data.splice(index2, 1)
    },
    handlechangeSel: function (index, index2) {
      var selected = this.list[index].data[index2].select
      this.list[index].data[index2].select = selected ? false : true
    },
    handleSelectAll: function () {
      var selectedAll = !this.selectAll
      for (var i = 0; i < this.list.length; i++) {
        for (let index = 0; index < this.list[i].data.length; index++) {
          this.list[i].data[index].select = selectedAll
        }
      }
    }
  }
})

//除法
function accDiv(arg1, arg2) {
  var t1 = 0,
    t2 = 0,
    r1, r2;
  try {
    t1 = arg1.toString().split(".")[1].length
  } catch (e) {}
  try {
    t2 = arg2.toString().split(".")[1].length
  } catch (e) {}
  with(Math) {
    r1 = Number(arg1.toString().replace(".", ""))
    r2 = Number(arg2.toString().replace(".", ""))
    return accMul((r1 / r2), pow(10, t2 - t1));
  }
} /* 何问起 hovertree.com */
//乘法 
function accMul(arg1, arg2) {
  var m = 0,
    s1 = arg1.toString(),
    s2 = arg2.toString();
  try {
    m += s1.split(".")[1].length
  } catch (e) {}
  try {
    m += s2.split(".")[1].length
  } catch (e) {}
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}
//加法  
function accAdd(arg1, arg2) {
  var r1, r2, m, c;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  c = Math.abs(r1 - r2);
  m = Math.pow(10, Math.max(r1, r2));
  if (c > 0) {
    var cm = Math.pow(10, c);
    if (r1 > r2) {
      arg1 = Number(arg1.toString().replace(".", ""));
      arg2 = Number(arg2.toString().replace(".", "")) * cm;
    } else {
      arg1 = Number(arg1.toString().replace(".", "")) * cm;
      arg2 = Number(arg2.toString().replace(".", ""));
    }
  } else {
    arg1 = Number(arg1.toString().replace(".", ""));
    arg2 = Number(arg2.toString().replace(".", ""));
  }
  return (arg1 + arg2) / m;
}
//减法  
function Subtr(arg1, arg2) {
  var r1, r2, m, n;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
  n = (r1 >= r2) ? r1 : r2;
  return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
}